#include "network.h"
#include "ml307r.h"
#include "uart.h"


#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>


UART_Port_Handle_t CAT1_4G_UART_Handle={.Instance = USART1};
UART_Port_Handle_t CAT111_4G_UART_Handle;
UART_Port_Handle_t CAT122_4G_UART_Handle;

static void Network_Init(struct NETWORK_HANDLE_T *handle)
{
	handle->info.delay = 8000;
	handle->info.dev_step =NETWORK_ATI;
}
static void Network_Loop_s(struct NETWORK_HANDLE_T *handle)
{

}

static void Network_Loop_ms(struct NETWORK_HANDLE_T *handle)
{
	if(handle->info.delay)
	{
		handle->info.delay--;
	}
	if(handle->info.dev_step !=DEVICE_NORMAL)
	{
		ML307R_Handle.AT_Step_Send(handle);
		ML307R_Handle.AT_Step_Parse(handle);
	}
}

#define UART_TX_LEN 256
static void Printf(char * string, ...)
{
		va_list arg;
	char _txBuffer[UART_TX_LEN];
	
	if(string==NULL)  return;
       if(strlen(string)>=UART_TX_LEN)
	   	return; 
	va_start(arg, string);
	memset(_txBuffer,0,UART_TX_LEN);
	vsprintf((char *)_txBuffer, string, arg);
	va_end(arg);
	CAT1_4G_UART_Handle.Send((uint8_t*)_txBuffer,strlen(_txBuffer));

}


void Network_Handle_Init_Info(Network_Handle_t *handle)
{
	if(handle == NULL)return;
	if(CAT1_4G_UART_Handle.Instance != USART1)
	{
		CAT1_4G_UART_Handle.Instance = USART1;
		USART_Init_config(115200,&CAT1_4G_UART_Handle);
		handle->UART_Handle = &CAT1_4G_UART_Handle;
	}
	else	if(CAT111_4G_UART_Handle.Instance != USART1 && (&Network_Handle11 == handle))
	{
		CAT111_4G_UART_Handle.Instance = USART1;
		USART_Init_config(115200,&CAT111_4G_UART_Handle);
		handle->UART_Handle = &CAT111_4G_UART_Handle;
	}
	else	if(CAT122_4G_UART_Handle.Instance != USART1 &&(&Network_Handle33 == handle))
	{
		CAT122_4G_UART_Handle.Instance = USART1;
		USART_Init_config(115200,&CAT122_4G_UART_Handle);
		handle->UART_Handle = &CAT122_4G_UART_Handle;
	}
	else
	{
		handle->UART_Handle = &CAT1_4G_UART_Handle;
	}	
	
}

Network_Handle_t Network_Handle=
{
	.Init = Network_Init,
	.Loop_s = Network_Loop_s,
	.Loop_ms = Network_Loop_ms,
	.Printf = Printf
};
Network_Handle_t Network_Handle11=
{
	.Init = Network_Init,
	.Loop_s = Network_Loop_s,
	.Loop_ms = Network_Loop_ms,
	.Printf = Printf
};
Network_Handle_t Network_Handle22=
{
	.Init = Network_Init,
	.Loop_s = Network_Loop_s,
	.Loop_ms = Network_Loop_ms,
	.Printf = Printf
};
Network_Handle_t Network_Handle33=
{
	.Init = Network_Init,
	.Loop_s = Network_Loop_s,
	.Loop_ms = Network_Loop_ms,
	.Printf = Printf
};


